package service

import (
	"context"
	"fmt"
	"log"
	__ "shop/shop-srv/handler/proto"
	"shop/shop-srv/pkg"
	"shop/shop-srv/service/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedShopServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) ShopAdd(_ context.Context, in *__.ShopAddReq) (*__.ShopAddResp, error) {
	str := ""
	if str == "1" {
		log.Fatalln("待审核")
	}
	if str == "2" {
		log.Fatalln("审核中")
	}
	if str == "3" {
		log.Fatalln("通过")
	}
	if str == "4" {
		log.Fatalln("驳回")
	}
	shop := model.Shop{
		Name:   in.Name,
		Price:  int(in.Price),
		Number: int(in.Number),
		Sales:  int(in.Sales),
		Image:  in.Image,
		Status: str,
	}
	baidu := pkg.NewBaidu(in.Name)
	if baidu != true {
		fmt.Println("表题中含有敏感词")
	}
	if model.ShopAdd(&shop) != nil {
		return nil, fmt.Errorf("添加失败")
	}
	return &__.ShopAddResp{
		Id: int64(shop.ID),
	}, nil
}
func (s *Server) MessageList(_ context.Context, in *__.MessageListReq) (*__.MessageListResp, error) {
	var message model.Message
	if model.MessageList(&message, int(in.Id)) != nil {
		return nil, fmt.Errorf("查询失败")
	}
	return &__.MessageListResp{
		ShopId: int64(message.ShopId),
		Name:   message.Name,
		Expert: message.Expert,
		Status: message.Status,
	}, nil
}
func (s *Server) ShopUpDate(_ context.Context, in *__.ShopUpDateReq) (*__.ShopUpDateResp, error) {
	var shop model.Shop
	if model.ShopUpDate(&shop, int(in.Id)) != nil {
		return nil, fmt.Errorf("同步失败")
	}
	return &__.ShopUpDateResp{
		Id: int64(shop.ID),
	}, nil
}
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	var user model.User
	if model.Login(&user) != nil {
		return nil, fmt.Errorf("登录失败")
	}
	return &__.LoginResp{
		Id: int64(user.ID),
	}, nil
}
func (s *Server) OrderList(_ context.Context, in *__.OrderListReq) (*__.OrderListResp, error) {
	var order model.Order
	if model.OrderList(&order, int(in.Id)) != nil {
		return nil, fmt.Errorf("校验失败")
	}
	return &__.OrderListResp{
		ShopId:    int64(order.ShopId),
		UserId:    int64(order.UserId),
		PlaceTime: order.PlaceTime,
		Status:    order.Status,
	}, nil
}
